using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

using umlungu.engineX.authoriserX.Cactus.Subscriber;
using umlungu.engineX.authoriserX.PassportX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-07-18	
	Status:		release	
	Version:	0.0.5
	Build:		20070628
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070628:	Added getContractDetails (+ added call in _GetAccount)
	20080409:	Remove Cactus.Provisioner (deprecated)
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.provisionerX {
	/// <summary>Description of the enumeration</summary>
	public enum x_authoriserID {
		/// <summary>Default authoriser</summary>
	    Default = 0,
		/// <summary>GateKeeper authoriser</summary>
	    GateKeeper = 1,
		/// <summary>PassportX authoriser</summary>
	    PassportX = 2,
		/// <summary>Cactus authoriser</summary>
		Cactus = 3
	}
	/// <summary>
	/// Description of the x_authoriser class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class x_authoriser {
		#region Invisible properties
		private x_authoriserID type;
		private x_config config;
		#endregion

		#region String constants
		private const string CONFIG_ROOT = "ProvisionerX/AuthoriserX/";
		private const string DEFAULT_USERNAME = "sa";
		private const string DEFAULT_PASSWORD = "password";
		private const string NAME_SERVICES = "services";
		private const string NAME_SERVICE = "service";
		private const string NAME_SERVICE_PARAMETER = "parameter";
		private const string NAME_SERVICE_PARAM = "param";
		private const string NAME_SUBSCRIBER = "subscriber";
		private const string NAME_CONTRACT = "contract";
		private const string NAME_CONTRACT_DETAIL = "contractdetail";
		#endregion

		#region Error constants
		private const string error_cactus_credentials = "Error in Cactus credentials: ";
		// message string from cactus when credentials unknown
		private const string CACTUS_CREDENTIALS_ERROR = "Server was unable to process request. --> Incorrect username or password";
		private const string CACTUS_SERVPROVIDER_ERROR = "Please contact your service provider";
		
		private const string error_cactus_login = "Error logging on to Cactus: ";
		private const string error_gatekeeper_login = "Error logging on to GateKeeper: ";
		
		private const string error_cactus_services = "Error getting Cactus services: ";
		private const string error_gatekeeper_services = "Error getting GateKeeper services: ";
		
		private const string error_cactus_addservice = "Error adding Cactus service: ";
		private const string error_gatekeeper_addservice = "Error adding GateKeeper service: ";
		
		private const string error_cactus_subscriber = "Error getting Cactus subscriber details: ";
		private const string error_cactus_serviceprovider = "Service provider subscriber error: ";
		private const string error_cactus_contract = "Error getting Cactus contract details: ";
		
		private const string error_cactus_addvoucher = "Error adding Cactus voucher: ";
		private const string error_gatekeeper_addvoucher = "Error adding GateKeeper voucher: ";
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_authoriser() {
			initialise(x_authoriserID.Default);
		}
		/// <summary>Constructor description</summary>
		/// <param name="AuthoriserType">The type of Authoriser</param>
		public x_authoriser(x_authoriserID AuthoriserType) {
			initialise(AuthoriserType);
		}
		#endregion

		#region Public methods
		/// <summary>Login to Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status Login(string username, string password) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		authorise(new Customer(), status, username, password);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	authorise(new NMCoreX(), status, username, password);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Get contract number of the account from Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status GetContract(string username, string password) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		getContract(new Customer(), status, username, password);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	getContract(new NMCoreX(), status, username, password);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Get account details from Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status GetAccount(string username, string password) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		string accno = getAccount(new Customer(), status, username, password, 0);
													Customer customerWS = new Customer();
													getAccountDetails(customerWS, status, username, password, accno);
													getContractDetails(customerWS, status, username, password, accno);
													break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	getAccount(new NMCoreX(), status, username, password, 0);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Get account details from Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status GetAccountService(string username, string password, int serviceid) {
			return GetAccountServices(username, password, serviceid);
		}
		/// <summary>Get account services details from Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status GetAccountServices(string username, string password) {
			return GetAccountServices(username, password, 0);
		}
		/// <summary>Get account services details from Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		public x_status GetAccountServices(string username, string password, int serviceid) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		string accno = getAccountServices(new Customer(), status, username, password, serviceid);	
													break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	getAccountServices(new NMCoreX(), status, username, password, serviceid);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Get account services from Authoriser service</summary>
		public x_status GetServices() {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		getServices(new Customer(), status);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	getServices(new NMCoreX(), status);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Add service to Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		/// <param name="serviceID">The service identifier</param>
		public x_status AddService(string username, string password, int serviceID) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		addService(new Customer(), status, username, password, serviceID);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	addService(new NMCoreX(), status, username, password, serviceID);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Add service to Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		/// <param name="serviceID">The service identifier</param>
		public x_status AddService(string username, string password, int serviceID, string paramName, string paramValue) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		addService(new Customer(), status, username, password, serviceID, paramName, paramValue);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	addService(new NMCoreX(), status, username, password, serviceID);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Add voucher to Authoriser service</summary>
		/// <param name="username">The user identity</param>
		/// <param name="password">The user password</param>
		/// <param name="serviceID">The service identifier</param>
		/// <param name="voucher">The service identifier</param>
		public x_status AddVoucher(string username, string password, int ServiceLinkID, double voucher) {
			x_status status = new x_status();
			try {
				switch (type) {
					case x_authoriserID.Cactus:		addVoucher(new Customer(), status, username, password, ServiceLinkID, voucher);	break;
					case x_authoriserID.GateKeeper:	
					case x_authoriserID.Default:	
										default:	addVoucher(new NMCoreX(), status, username, password, ServiceLinkID, voucher);	break;
				}
			} catch (x_exception e) {
				status.AddError(e);
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Test method</summary>
		/// <param name="parameter">Brief description of the parameter</param>
		public x_status LoginTest(string username, string password) {
			x_status status = new x_status();
			try {
				//object[] results = this.Invoke("Login", new object[] { username, password });
				NMCoreX thisservice = new NMCoreX();
				thisservice.Login(username, password);
				status.AddOk();
			} catch (Exception e) {
				status.AddError(e);
			}
	        return status;
		}
		/// <summary>Test method</summary>
		public x_status TestOK() {
			x_status status = new x_status();
			status.AddOk();
	        return status;
		}
		#endregion
		
		#region Private methods
		/// <summary>Initialises any utilities within the class</summary>
		private void initialise(x_authoriserID typ) {
			type = typ;
			config = new x_config();
		}
		
		private void authorise(Customer webservice, x_status status, string uname, string pword) {
			try {
				webservice.Url = getUrl("subscriber");
				string errormsg;
				string contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out errormsg);
				if (contractno == "")
					throw new Exception(String.Concat(errormsg, " [", uname, "]"));
				else
					status.AddOk("ContractNo", contractno);
			} catch (Exception e) {
				checkCredentials(e.Message);
				throw new x_exception("error_cactus_login", String.Concat(error_cactus_login, e.Message, "[", webservice.Url, "]"));
			}
		}
		
		private void authorise(NMCoreX webservice, x_status status, string uname, string pword) {
			try {
				webservice.Url = getUrl();
				XmlElement result = webservice.Login(uname, pword) as XmlElement;
				if (result.SelectSingleNode("//Result/ResultCode").InnerText != "0")
					status.AddError(result.SelectSingleNode("//Result/Description").InnerText);
				else
					status.AddOk(result);
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_login", String.Concat(error_gatekeeper_login, e.Message));
			}
		}
		
		private string getContract(Customer webservice, x_status status, string uname, string pword) {
			string contractno = "";
			try {
				webservice.Url = getUrl("subscriber");
				string errormsg;
				contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out errormsg);
				if (contractno == "")
					throw new Exception(String.Concat(errormsg, " [", uname, "]"));
				else
					status.AddOk(NAME_CONTRACT, contractno);
			} catch (Exception e) {
				checkCredentials(e.Message);
				throw new x_exception("error_cactus_login", String.Concat(error_cactus_login, e.Message));
			}
			return contractno;
		}
		
		// null functionality at present
		private string getContract(NMCoreX webservice, x_status status, string uname, string pword) {
			string contractno = "";
			try {
				webservice.Url = getUrl();
				XmlElement result = webservice.Login(uname, pword) as XmlElement;
				if (result.SelectSingleNode("//Result/ResultCode").InnerText != "0")
					status.AddError(result.SelectSingleNode("//Result/Description").InnerText);
				else
					status.AddOk(result);
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_login", String.Concat(error_gatekeeper_login, e.Message));
			}
			return contractno;
		}
		
		/// <overloads></overloads>
		/// <summary></summary>
		private string getAccount(Customer webservice, x_status status, string uname, string pword, int servno) {
			string contractno = "";
			try {
				webservice.Url = getUrl("subscriber");
				string errormsg;
				contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out errormsg);
				if (contractno == "")
					//status.AddError(errormsg);
					throw new Exception(String.Concat(errormsg, " [", uname, "]"));
				else {
					status.AddOk(NAME_CONTRACT, contractno);
					ServiceInfo[] result = webservice.GetLoadedServices(getCactusCredentials(), uname, pword, contractno, out errormsg);
					status.AddNode("message", errormsg);
					try {
						XmlElement servs = status.AddElem(NAME_SERVICES);
						foreach (ServiceInfo serv in webservice.GetLoadedServices(getCactusCredentials(), uname, pword, contractno, out errormsg)) {
							if (servno == 0 || servno == serv.ServiceNumber) {
								XmlElement servel = status.AddElem(servs, NAME_SERVICE);
								servel.SetAttribute("startdate", serv.StartDate);
								servel.SetAttribute("enddate", serv.StartDate);
								servel.SetAttribute("linkid", serv.ServiceLinkNumber.ToString());
								servel.SetAttribute("id", serv.ServiceNumber.ToString());
								foreach (ServiceParameter param in serv.Parameters) {
									XmlElement paramel = status.AddElem(servel, NAME_SERVICE_PARAMETER);
									paramel.SetAttribute("name", param.Name);
									paramel.InnerText = param.Value;
								}
							}
						}
					}
					catch(System.Exception e) {
						throw(new x_exception("error_cactus_services", String.Concat(error_cactus_services, e.Message)));
					}
				}
			} catch (Exception e) {
				checkCredentials(e.Message);
				checkCactusServiceProvider(e);
				throw new x_exception("error_cactus_login", String.Concat(error_cactus_login, e.Message));
			}
			return contractno;
		}
		
		/// <summary></summary>
		private void getAccountDetails(Customer webservice, x_status status, string uname, string pword, string contractno) {
			try {
				webservice.Url = getUrl("subscriber");

				umlungu.engineX.authoriserX.Cactus.Subscriber.SubscriberDetail detail = webservice.GetSubscriberDetails(getCactusCred(), uname, pword, contractno);
				XmlElement subscriber = status.AddElem(NAME_SUBSCRIBER);
				subscriber.SetAttribute("Firstname", detail.Name);
				subscriber.SetAttribute("Surname", detail.Surname);
				subscriber.SetAttribute("Email", detail.EmailAddress);
				subscriber.SetAttribute("CellPhone", detail.TelCell);
			} catch (Exception e) {
				checkCactusServiceProvider(e);
				throw new x_exception("error_cactus_subscriber", String.Concat(error_cactus_subscriber, e.Message, " ::for '", uname, "' [*****]"));
			}
		}
		
		/// <summary></summary>
		private void getContractDetails(Customer webservice, x_status status, string uname, string pword, string contractno) {
			try {
				webservice.Url = getUrl("subscriber");
				
				ContractDetail[] details = webservice.GetContractDetails(getCactusCred(), uname, pword, contractno);
				foreach (ContractDetail detail in details) {
					XmlElement contract = status.AddElem(NAME_CONTRACT_DETAIL);
					contract.SetAttribute("ContractNumber", detail.ContractNumber);
					contract.SetAttribute("Package", detail.Package);
					contract.SetAttribute("ActivationDate", detail.ActivationDate);
					contract.SetAttribute("Period", detail.Period.ToString());
					contract.SetAttribute("Status", detail.Status);
				}
			} catch (Exception e) {
				checkCactusServiceProvider(e);
				throw new x_exception("error_cactus_contract", String.Concat(error_cactus_contract, e.Message, " ::for '", uname, "' [******]"));
			}
		}
		
		/// <summary></summary>
		// null functionality at present
		private string getAccount(NMCoreX webservice, x_status status, string uname, string pword, int servno) {
			string accountno = "";
			try {
				webservice.Url = getUrl();
				XmlElement result = webservice.Login(uname, pword) as XmlElement;
				if (result.SelectSingleNode("//Result/ResultCode").InnerText != "0")
					status.AddError(result.SelectSingleNode("//Result/Description").InnerText);
				else
					status.AddOk(result);
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_login", String.Concat(error_gatekeeper_login, e.Message));
			}
			return accountno;
		}
		
		/// <overloads></overloads>
		/// <summary></summary>
		private string getAccountServices(Customer webservice, x_status status, string uname, string pword, int servno) {
			string contractno = "";
			try {
				webservice.Url = getUrl("subscriber");
				string errormsg;
				contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out errormsg);
				if (contractno == "")
					//status.AddError(errormsg);
					throw new Exception(String.Concat(errormsg, " [", uname, "]"));
				else {
					status.AddOk(NAME_CONTRACT, contractno);
					ServiceInfo[] result = webservice.GetLoadedServices(getCactusCredentials(), uname, pword, contractno, out errormsg);
					status.AddNode("message", errormsg);
					try {
						XmlElement servs = status.AddElem(NAME_SERVICES);
						foreach (ServiceInfo serv in webservice.GetLoadedServices(getCactusCredentials(), uname, pword, contractno, out errormsg)) {
							if (servno == 0 || servno == serv.ServiceNumber) {
								XmlElement servel = status.AddElem(servs, NAME_SERVICE);
								servel.SetAttribute("startdate", serv.StartDate);
								servel.SetAttribute("enddate", serv.StartDate);
								servel.SetAttribute("linkid", serv.ServiceLinkNumber.ToString());
								servel.SetAttribute("id", serv.ServiceNumber.ToString());
								foreach (ServiceParameter param in serv.Parameters) {
									XmlElement paramel = status.AddElem(servel, NAME_SERVICE_PARAMETER);
									paramel.SetAttribute("name", param.Name);
									paramel.InnerText = param.Value;
								}
							}
						}
					}
					catch(System.Exception e) {
						throw(new x_exception("error_cactus_services", String.Concat(error_cactus_services, e.Message)));
					}
				}
			} catch (Exception e) {
				checkCredentials(e.Message);
				checkCactusServiceProvider(e);
				throw new x_exception("error_cactus_login", String.Concat(error_cactus_login, e.Message));
			}
			return contractno;
		}
		
		/// <summary></summary>
		// null functionality at present
		private string getAccountServices(NMCoreX webservice, x_status status, string uname, string pword, int servno) {
			string accountno = "";
			try {
				webservice.Url = getUrl();
				XmlElement result = webservice.Login(uname, pword) as XmlElement;
				if (result.SelectSingleNode("//Result/ResultCode").InnerText != "0")
					status.AddError(result.SelectSingleNode("//Result/Description").InnerText);
				else
					status.AddOk(result);
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_login", String.Concat(error_gatekeeper_login, e.Message));
			}
			return accountno;
		}
		
		/// <overloads></overloads>
		/// <summary></summary>
		private void getServices(Customer webservice, x_status status) {
			try {
				webservice.Url = getUrl("subscriber");
				status.AddOk();
				XmlElement servs = status.AddElem(NAME_SERVICES);
				foreach (ServiceType serv in webservice.GetServiceTypes(getCactusCredentials())) {
					int id = serv.ID;
					string name = serv.Description;
					XmlElement servel = status.AddElem(servs, NAME_SERVICE);
					servel.SetAttribute("name", name);
					servel.SetAttribute("id", id.ToString());
					foreach (string param in serv.Parameters) {
						XmlElement paramel = status.AddElem(servel, NAME_SERVICE_PARAMETER);
						paramel.SetAttribute("name", param);
					}
					/* for WBSWebService only?
					foreach (string parm in serv..parameters) {
						XmlElement paramel = status.AddElem(servel, NAME_SERVICE_PARAM);
						paramel.SetAttribute("name", parm);
					}
					*/
				}
			} catch (Exception e) {
				throw new x_exception("error_cactus_services", String.Concat(error_cactus_services, e.Message));
			}
		}
		
		/// <summary></summary>
		// null functionality at present
		private void getServices(NMCoreX webservice, x_status status) {
			try {
				webservice.Url = getUrl();
				status.AddOk();
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_services", String.Concat(error_gatekeeper_services, e.Message));
			}
		}
		
		/// <overloads></overloads>
		/// <summary></summary>
		private void addService(Customer webservice, x_status status, string uname, string pword, int servid, ServiceParameter[] parameters) {
			try {
				webservice.Url = getUrl("subscriber");
				string infomsg;
				string contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out infomsg);
				if (contractno == "")
					status.AddError("error_cactus_login", String.Concat(error_cactus_login, infomsg));
				else {
					if (webservice.AddServiceToContract(getCactusCredentials(), uname, pword, contractno, servid, parameters, out infomsg)) {
						status.AddOk("email", infomsg);	// when OK, then contains client's account email (for messaging)
					} else
						status.AddError(infomsg);
				}
			} catch (Exception e) {
				throw new x_exception("error_cactus_addservice", String.Concat(error_cactus_addservice, e.Message));
			}
		}
		private void addService(Customer webservice, x_status status, string uname, string pword, int servid) {
			ServiceParameter[] parameters = new ServiceParameter[] { };	// cannot pass null
			addService(webservice, status, uname, pword, servid, parameters);
		}
		private void addService(Customer webservice, x_status status, string uname, string pword, int servid, string paramName, string paramValue) {
			Array parameters = Array.CreateInstance(typeof(ServiceParameter), 1);
			ServiceParameter param = new ServiceParameter();
			param.Name = paramName;
			param.Value = paramValue;
			parameters.SetValue(param, 0);
			addService(webservice, status, uname, pword, servid, (ServiceParameter[])parameters);
		}
		
		/// <summary></summary>
		// null functionality at present
		private void addService(NMCoreX webservice, x_status status, string uname, string pword, int servid) {
			try {
				webservice.Url = getUrl();
				status.AddOk();
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_addservice", String.Concat(error_gatekeeper_addservice, e.Message));
			}
		}
		
		/// <overloads></overloads>
		/// <summary></summary>
		private void addVoucher(Customer webservice, x_status status, string uname, string pword, int servid, double amount) {
			try {
				webservice.Url = getUrl("subscriber");
				string infomsg;
				string contractno = webservice.ContractLogin(getCactusCredentials(), uname, pword, out infomsg);
				if (contractno == "")
					status.AddError("error_cactus_login", String.Concat(error_cactus_login, infomsg));
				else {
					if (webservice.AddVoucherToService(getCactusCredentials(), uname, pword, contractno, servid, amount, out infomsg)) {
						status.AddOk("voucher", amount.ToString());
					} else
						throw new Exception(infomsg);
						//status.AddError(infomsg);
				}
			} catch (Exception e) {
				throw new x_exception("error_cactus_addvoucher", String.Concat(error_cactus_addvoucher, e.Message));
			}
		}
		
		/// <summary></summary>
		private void addVoucher(NMCoreX webservice, x_status status, string uname, string pword, int servid, double amount) {
			try {
				webservice.Url = getUrl();
				status.AddOk();
			} catch (Exception e) {
				throw new x_exception("error_gatekeeper_addvoucher", String.Concat(error_gatekeeper_addvoucher, e.Message));
			}
		}
		
		/// <overloads>Sets the login credentials for access te authoriser </overloads>
		/// <summary></summary>
		private ServiceProviderLoginDetails getCactusCredentials(string username, string password) {
			ServiceProviderLoginDetails loginDetail = new ServiceProviderLoginDetails();
			loginDetail.UserName = username;
			loginDetail.Password = password;
			return loginDetail;
		}
		/// <summary></summary>
		private ServiceProviderLoginDetails getCactusCredentials() {
			XmlElement cactusel = (XmlElement) config.Element(String.Concat(CONFIG_ROOT, type.ToString()));
			return getCactusCredentials(cactusel.GetAttribute("username"), cactusel.GetAttribute("password"));
		}
		/// <summary></summary>
		private umlungu.engineX.authoriserX.Cactus.Subscriber.ServiceProviderLoginDetails getCactusCred() {
			XmlElement cactusel = (XmlElement) config.Element(String.Concat(CONFIG_ROOT, type.ToString()));
			return getCactusCred(cactusel.GetAttribute("username"), cactusel.GetAttribute("password"));
		}
		/// <summary></summary>
		private umlungu.engineX.authoriserX.Cactus.Subscriber.ServiceProviderLoginDetails getCactusCred(string username, string password) {
			umlungu.engineX.authoriserX.Cactus.Subscriber.ServiceProviderLoginDetails loginDetail = new umlungu.engineX.authoriserX.Cactus.Subscriber.ServiceProviderLoginDetails();
			loginDetail.UserName = username;
			loginDetail.Password = password;
			return loginDetail;
		}
		/// <summary></summary>
		private void checkCredentials(string exceptionMessage) {
			if (exceptionMessage == CACTUS_CREDENTIALS_ERROR)
				throw new x_exception("error_cactus_credentials", String.Concat(error_cactus_credentials, exceptionMessage));
		}
		/// <overloads>Gets the url for the webservice from the config element</overloads>
		/// <summary></summary>
		private string getUrl() {
			return config.Value(String.Concat(CONFIG_ROOT, type.ToString(), "/Url"));
		}
		private string getUrl(string id) {
			return config.Value(String.Concat(CONFIG_ROOT, type.ToString(), "/Url[@id='", id, "']"));
		}
		/// <summary>Checks the special case of unabel to login because the account belongs to a service provider</summary>
		private void checkCactusServiceProvider(Exception e) {
			int start = e.Message.IndexOf(CACTUS_SERVPROVIDER_ERROR);
			if (start >= 0)	{
				start = e.Message.IndexOf("(")+1;
				throw new x_exception("error_cactus_serviceprovider", e.Message.Substring(start, e.Message.IndexOf(")") - start));
			}
		}
		#endregion
	}
}
